package system.menus;

import java.util.List;
import javax.el.ELContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import system.general.Logged;

/**
 *
 * @author BENIGNO
 */
@ManagedBean
@RequestScoped
public class Main {

    private MenuModel menuModel;
    private Find menuDAO = new Find();
    private Logged logged;

    public void geraMenu() {
        menuModel = new DefaultMenuModel();
        List<Bean> listaMenu = menuDAO.listaTodos(); // Substituir o listaTodos() pelos menus permitidos
        for (Bean menu : listaMenu) {
            if (menu.getMenu().getId() == 0) {
                Submenu submenu = new Submenu();
                submenu.setId("s" + submenu.getClientId());
                submenu.setIcon("ui-icon-arrowreturnthick-1-s");
                submenu.setLabel(menu.getNombre());
                for (Bean m : menuDAO.buscaPorMenu(menu)) {
                    if (!menuDAO.verificaSubMenu(m)) {
                        MenuItem mi = new MenuItem();
                        mi.setValue(m.getNombre());
                        mi.isGlobal();
                        mi.setUrl(m.getUrl());
                        mi.setId("s" + mi.getClientId());
                        mi.setTitle(m.getNombre());
                        mi.setIcon("ui-icon-arrowreturnthick-1-e");
                        submenu.getChildren().add(mi);
                    } else {
                       MenuItem mi = new MenuItem();
                        mi.setValue(m.getNombre());
                        mi.isGlobal();
                        mi.setUrl(m.getUrl());
                        mi.setId("s" + mi.getClientId());
                        mi.setTitle(m.getNombre());
                        mi.setIcon("ui-icon-arrowreturnthick-1-e");;
                     menuModel.addMenuItem(mi);
                    }
                }
                menuModel.addSubmenu(submenu);
            }
        }
    }

    public Submenu geraSubmenu(Bean menu) {
        Submenu submenu = new Submenu();
        submenu.setId("s" + submenu.getClientId());
        submenu.setLabel(menu.getNombre());
        for (Bean m : menuDAO.buscaPorMenu(menu)) {
            if (!menuDAO.verificaSubMenu(m)) {
                MenuItem mi = new MenuItem();
                mi.setValue(m.getNombre());
                mi.setUrl(m.getUrl());
                mi.setId("s" + mi.getClientId());
                submenu.getChildren().add(mi);
            } else {
                submenu.getChildren().add(geraSubmenu(m));
            }
        }
        return submenu;
    }

    public MenuModel getMenuModel() {
    // logged.clearMenu();
        return menuModel;
    }

    public Main() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        logged = (Logged) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "logged");
        geraMenu();
    }
}
